package com.awspaas.user.apps.processforsap.pluginImpl;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.bpmn.engine.model.run.delegate.ProcessInstance;
import com.actionsoft.bpms.commons.at.AbstExpression;
import com.actionsoft.bpms.commons.at.ExpressionContext;
import com.actionsoft.sdk.local.SDK;
import org.apache.commons.lang3.StringUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class GetContractName extends AbstExpression {
    public GetContractName(final ExpressionContext atContext, String expressionValue) {
        super(atContext, expressionValue);
    }

    @Override
    public String execute(String expression) {
        ExpressionContext result = getExpressionContext();
        ProcessInstance processInstance = result.getProcessInstance();
        BO boMain = SDK.getBOAPI().query("BO_EU_SAPSD_KHSWXGXYPS").detailByBindId(processInstance.getId());
        String jglx = boMain.getString("FIELD0006");
        List<BO> list = SDK.getBOAPI().query("BO_EU_SAP_SWLIST").bindId(processInstance.getId()).list();
        //判断有几个主体
        Set<String> ztList = new HashSet<>();
        for (BO bo1 : list) {
            String yxqyzt = bo1.getString("YXQYZT");
            ztList.add(yxqyzt);
        }
        if (ztList.size() == 1) {
            String result111 = "";
            String result111222 = "";
            for (String zt : ztList) {
                if ("FS00".equals(zt)) {
                    result111 = result111 + "FESC-";
                } else if ("FT00".equals(zt)) {
                    result111 = result111 + "FHHK-";
                } else {
                    result111 = result111 + "FHEC-";
                }
                for (BO bo1 : list) {
                    String custcode = bo1.getString("CUSTCODE");
                    String custname = bo1.getString("CUSTNAME");
                    if (StringUtils.isNotEmpty(custcode)){
                        result111 = result111 + custcode + "-";
                    }
                    result111222 = result111222 + custcode +custname+ "-";
                }
                result111222 = result111222.substring(0,result111222.length()-1);
//                result111 = result111.substring(0, result111.length() - 1);
                String lsm = "@ifThen(" + zt + ",FH00,@sequence(KH,5,0),FS00,@sequence(BBTKH,5,0),FT00,@sequence(XGKH,5,0))";
                Object resultLsh = SDK.getProcessAPI().getVariable(processInstance,"lsh");
                String lsh = "";
                if (resultLsh==null){
                    lsh =  SDK.getRuleAPI().executeAtScript("@replace(@date,\\-,)") + SDK.getRuleAPI().executeAtScript(lsm);
                    SDK.getProcessAPI().setVariable(processInstance,"lsh",lsh);
                }else{
                    lsh = String.valueOf(resultLsh);
                }
                String result111aaaaa = (result111 +lsh).replace("-","");

                return result111aaaaa+"&&"+result111+lsh+"-"+jglx+"-"+result111222;
            }
        } else {
            String result111 = "FHEC-";
            String result111222 = "";
            for (BO bo1 : list) {
                String custcode = bo1.getString("CUSTCODE");
                String custname = bo1.getString("CUSTNAME");
                result111 = result111 + custcode + "-";
                if (StringUtils.isNotEmpty(custcode)){
                    result111 = result111 + custcode + "-";
                }
                result111222 = result111222 + custcode +custname+ "-";
            }
            result111222 = result111222.substring(0,result111222.length()-1);
//            result111 = result111.substring(0, result111.length() - 1);
            String lsm = "@sequence(KH,5,0)";
            Object resultLsh = SDK.getProcessAPI().getVariable(processInstance,"lsh");
            String lsh = "";
            if (resultLsh==null){
                lsh =  SDK.getRuleAPI().executeAtScript("@replace(@date,\\-,)") + SDK.getRuleAPI().executeAtScript(lsm);
                SDK.getProcessAPI().setVariable(processInstance,"lsh",lsh);
            }else{
                lsh = String.valueOf(resultLsh);
            }
            String result111aaaaa = (result111 +lsh).replace("-","");

            return result111aaaaa+"&&"+result111+lsh+"-"+jglx+"-"+result111222;

        }
        return "";
    }
}
